<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <SelectsShopCascade v-model:platform="form.platform_id" v-model:site="form.site" v-model:shop="form.shop_id" />
        <el-select v-model="form.status" style="max-width: 200px" clearable placeholder="状态">
          <el-option v-for="(d, i) in statusOption" :key="i" :label="d.label" :value="d.value"></el-option>
        </el-select>
      </template>
      <template #toolbar_btns>
        <VButton type="primary" @click="add" v-hasPermi="[basicSetupAuthList.commission_seting.add]">
          <div class="dk-iconfont icon-Plus"></div>
          新增
        </VButton>
        <VButton @click="handleStatus(1)" v-hasPermi="[basicSetupAuthList.commission_seting.enable]">
          <div class="dk-iconfont icon-CircleCheck text-[#3487FF]"></div>
           启用
        </VButton>
        <VButton @click="handleStatus(0)" v-hasPermi="[basicSetupAuthList.commission_seting.disable]">
          <div class="dk-iconfont icon-Disabled text-[red]"></div>
          禁用
        </VButton>
      </template>
    </VTable>

    <!-- 日志/ -->
    <Log dialog v-model="logVisible" :autoLoad="false" tag="order_commission" :business_id="logId" />

    <Vedit ref="edit" :title="editTitle" class="dialogPublic" @confirm="editConfirm"> </Vedit>
  </VPage>
</template>
<script setup lang="jsx" name="basic-setupCommission-seting">
import { overallOptions } from "@/api/public"
import Vedit from "./components/edit"
import { queryList, commissionDelete, commissionStatus, oplog } from "./api"

import { ElMessage, ElMessageBox } from "element-plus"
import { onMounted, ref, toRefs } from "vue"
import SelectsShopCascade from "@/components/Selects/SelectsShopCascade/index.vue"
import { basicSetupAuthList } from "@/Auth/basic-setup.js"
const edit = ref()
const data = reactive({
  editTitle: "新增",
  statusOption: [],
})
const { editTitle, statusOption } = toRefs(data)
const tableRef = ref()

// 日志
import Log from "@/components/Log/index.vue"
const logRef = ref()
const logVisible = ref(false)
const logId = ref("")
const openLog = (row) => {
  logId.value = row.id
  logVisible.value = true
}

//编辑
const ruleEdit = (row) => {
  editTitle.value = "编辑"
  edit.value.open(row)
}
//新增
const add = () => {
  editTitle.value = "新增"
  edit.value.open()
}
const editConfirm = () => {
  query()
}
//删除
const profitSettingDeleteF = (id) => {
  ElMessageBox.confirm("此操作将永久删除该项, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    confirmButtonClass: "oms-message-confirm-button",
  }).then(async () => {
    try {
      commissionDelete({ id }).then((res) => {
        ElMessage({ message: "删除成功！", type: "success" })
        query()
      })
    } catch (e) {
      throw Error(e)
    }
  })
}
//启用
const handleStatus = (status) => {
  if (!getSelectRow().length) {
    return false
  }
  const arr = getSelectRow()
  commissionStatus({ id: arr, status }).then((res) => {
    ElMessage({ message: "操作成功", type: "success" })
    query()
  })
}

// 查询
const query = () => {
  tableRef?.value.query()
}
import { getAloneEnum } from "@/utils/enumUtils"
const originOption = ref({})
//获取下拉数据
onMounted(async () => {
  overallOptions({ params: ["base.orderCommission"] }).then((res) => {
    statusOption.value = res.data.baseOrderCommission.status
  })
  let optData = await getAloneEnum(["platform"])
  originOption.value.platform = Object.fromEntries(optData?.platform.map((v) => [v.value, v.label]))
})
//获取已选中的行
const getSelectRow = () => {
  const getCheckboxRecords = tableRef?.value.$table.getCheckboxRecords()
  if (!getCheckboxRecords.length) {
    ElMessage({ showClose: true, message: "请至少选择一条数据！", type: "warning" })
    return false
  }
  const arr = []
  getCheckboxRecords.map((e) => {
    arr.push(e.id)
  })
  return arr
}
// 列表配置
const tableOptins = reactive({
  formConfig: {
    data: {
      platform_id: "",
      site: [],
      shop: [],
      status: "",
      create_user: "",
      rule_type: "",
      rule_name: "",
    },
  },
  showOverflow: true,
  columns: [
    { type: "checkbox", fixed: "left", width: 60 },
    { field: "id", title: "ID", width: 80 },
    {
      field: "platform_id_desc",
      title: "平台",
      width: 100,
      slots: {
        default: ({ row }) => <VText value={originOption.value.platform?.[row.platform_id]} />,
      },
    },
    {
      field: "site",
      title: "站点",
      minWidth: 180,
      slots: {
        default: ({ row }) => <VText value={row?.site?.join()} />,
      },
    },
    { field: "lowest_selling_price", title: "最低订单金额", minWidth: 120 },
    { field: "highest_selling_price", title: "最高订单金额", minWidth: 140 },
    { field: "commission_rate", title: "佣金比例(%)", minWidth: 120 },
    { field: "buy_it_now_commission", title: "一口价佣金", minWidth: 120 },
    { field: "minimum_commission", title: "最低佣金", minWidth: 120 },
    { field: "additional_commission", title: "附加佣金", minWidth: 120 },
    {
      title: "状态",
      field: "status_text",
      width: 100,
      slots: {
        default: ({ row }) => <VStatus value={!!row.status} />,
      },
    },
    { field: "create_user", title: "创建人", width: 150 },
    { field: "create_time", title: "创建时间", width: 170 },
    {
      title: "操作",
      width: 150,
      fixed: "right",
      slots: {
        default: ({ row }) => {
          return [
            <div>
              <el-button link type="primary" onClick={() => ruleEdit(row)} v-hasPermi={[[basicSetupAuthList.commission_seting.edit]]}>
                编辑
              </el-button>
              <el-button link type="primary" onClick={() => openLog(row)} v-hasPermi={[[basicSetupAuthList.commission_seting.log]]}>
                日志
              </el-button>
              <el-button link type="danger" onClick={() => profitSettingDeleteF(row.id)} v-hasPermi={[[basicSetupAuthList.commission_seting.delete]]}>
                删除
              </el-button>
            </div>,
          ]
        },
      },
    },
  ],
  proxyConfig: {
    ajax: {
      query: ({ page, form }) => queryList({ ...form, ...page }),
    },
  },
})
</script>
<style lang="scss" scoped></style>
